<?php

/** 
* 
* 
* by:南宁火蝶科技有限公司 http:///www.HdsysCms.com
* 声明:本系统禁止用二次开发、侵犯HdsysCms著作和除去HdsysCms进行售卖,如发现将追究一切权利,模版制作属于自己所得可以通过本系统对外开发模版获取收益!
* 
* 2019年第一版本发布 - 到2023.9.1开源使用! 
* php混淆工具:phpencry.wbox.top
* 
* 注意防骗:@HdsysCms 官方没授权任何个人机构合作,唯一收款抬头:《南宁火蝶科技有限公司》和官网显示客服唯一渠道客服,其它渠道以HdsysCms名义行为均骗子!
*
*/
	

defined('HDSYS_SET') or call_my_die("" . 'Not,This File Not Can in Ie Open');
if (HDSYS_SET !='ADMIN') {
call_my_die("" . '访问路径错误');
}
class Template
{
public function __construct()
{
$this->intelligence_session=bs_lib::load_libhdsys_class("" . 'session');
$this->intelligence_session->intelligence_session_open();
$this->intelligence_db=bs_lib::load_libhdsys_class(templates_Verifications_db());
$this->purconfig=bs_lib::load_libhdsys_class('purconfig');
bs_lib::load_modules_common();
isadminlogin();
}
function call_back_template()
{
include bs_lib::load_default_path();
}
function call_back_template_ajax()
{
$param_file=set_post('file');
$param_file_dir="templates/{$param_file}";
if (!class_exists('ZipArchive')) {
print_json(array(call_my_str_code()=> call_my_int_1(), call_my_str_msg()=> "ZipArchive 类不存在,你环境不支持打包备份.请手动备份:{$param_file_dir}", "url"=> ''));
}
$param_time=date('YmdHis');
$param_images_dir=HDSYS_DIR_SYS . $param_file_dir;
$param_zip_file="cache/{$param_file}_hdsyscms_{$param_time}.zip";
$this->Zip($param_images_dir, HDSYS_DIR_SYS . $param_zip_file);
print_json(array(call_my_str_code()=> call_my_int_1(), call_my_str_msg()=> "ok", "url"=> getsysurl() . $param_zip_file));
}
function Zip($source, $destination)
{
if (!extension_loaded('zip') || !file_exists($source)) {
return false;
}
$zip=new ZipArchive();
if (!$zip->open($destination, ZIPARCHIVE::CREATE)) {
return false;
}
$source=str_replace('\\', '/', realpath($source));
if (is_dir($source)===true) {
$files=new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source), RecursiveIteratorIterator::SELF_FIRST);
foreach ($files as $file) {
$file=str_replace('\\', '/', $file);
if (in_array(substr($file, strrpos($file, '/') + 1), array('.', '..')))
continue;
$file=realpath($file);
if (is_dir($file)===true) {
$zip->addEmptyDir(str_replace($source . '/', '', $file . '/'));
} else if (is_file($file)===true) {
$zip->addFromString(str_replace($source . '/', '', $file), file_get_contents($file));
}
}
} else if (is_file($source)===true) {
$zip->addFromString(basename($source), file_get_contents($source));
}
return $zip->close();
}
function call_table()
{
include bs_lib::load_default_path();
}
function call_set_template()
{
$param_dir_name=set_post("" . 'dir_name');
$param_template=set_post("" . 'template');
if($param_template=='en'){
$param_bool=$this->purconfig->pur_configs('template', array('current_en'=> $param_dir_name));
}else{
$param_bool=$this->purconfig->pur_configs('template', array('current'=> $param_dir_name));
}
if ($param_bool) {
print_json(array(call_my_str_code()=> call_my_int_1(), call_my_str_msg()=> "设置模版成功,记得清理缓存!"));
} else {
print_json(array(call_my_str_code()=> call_my_int_aeg1(), call_my_str_msg()=> "保存失败,请确认缓存目录【cache】写入权限!"));
}
}
function call_template_open_ajax()
{
$param_open=set_post("" . 'open');
$param_dir=set_post("" . 'dir');
$param_dir=safe_replace_file($param_dir);
$param_host='../';
$param_url=call_my_template_open($param_open, $param_dir, $param_host);
print_json(array(call_my_str_code()=> call_my_int_1(), call_my_str_msg()=> "ok", "url"=> $param_host . $param_url . '?template=' . $param_dir));
}
function call_del_template_ajax()
{
$param_file=set_post('file');
$param_file=safe_replace_file($param_file);
if ($param_file=='') {
print_json(array(call_my_str_code()=> call_my_int_1(), call_my_str_msg()=> "模版空"));
}
$param_file_dir="templates/{$param_file}";
if (!file_exists(HDSYS_DIR_SYS . $param_file_dir)) {
print_json(array(call_my_str_code()=> call_my_int_1(), call_my_str_msg()=> "模版不存在," . $param_file_dir));
}
if ($this->deldir(HDSYS_DIR_SYS . $param_file_dir)) {
print_json(array(call_my_str_code()=> 1000, call_my_str_msg()=> "删除成功"));
} else {
print_json(array(call_my_str_code()=> call_my_int_1(), call_my_str_msg()=> "删除失败,或没权限请手动删除 模版目录路径:" . $param_file_dir));
}
}
public function deldir($dir)
{
if (is_dir($dir)) {
$dh=opendir($dir);
while ($file=readdir($dh)) {
if ($file !="." && $file !="..") {
$fullpath=$dir . "/" . $file;
if (!is_dir($fullpath)) {
@unlink($fullpath);
} else {
$this->deldir($fullpath);
}
}
}
closedir($dh);
if (@rmdir($dir)) {
return true;
} else {
return false;
}
}
}
function call_template_del_ajax()
{
$param_dir=set_post('dir');
$param_file=set_post('file');
$param_dir=safe_replace_file($param_dir);
$param_file=safe_replace_file($param_file);
$param_file="/templates/{$param_dir}/{$param_file}";
if (!file_exists(HDSYS_DIR_SYS  . $param_file)) {
print_json(array(call_my_str_code()=> call_my_int_aeg1(), call_my_str_msg()=> '文件不存在:' . $param_file));
}
if (@unlink(HDSYS_DIR_SYS  . $param_file)) {
print_json(array(call_my_str_code()=> 1000, call_my_str_msg()=> "删除成功"));
} else {
print_json(array(call_my_str_code()=> call_my_int_1(), call_my_str_msg()=> "删除失败"));
}
}
function call_up_ajax()
{
if (!class_exists('ZipArchive')) {
print_json(array(call_my_str_code()=> call_my_int_1(), call_my_str_msg()=> "ZipArchive 类不存在,可以手动上传模版到模版目录", "url"=> ''));
}
$param_File_Name=$_FILES['file']['name'];
$temp_arr=explode('.', $param_File_Name);
$param_file_ext=array_pop($temp_arr);
if ($param_file_ext !="zip") {
print_json(array(call_my_str_code()=> call_my_int_aeg1(), call_my_str_msg()=> '上传的文件类型不符zip 你上传类型:' . $param_file_ext));
}
$param_zip=new ZipArchive();
$param_opens=$param_zip->open($_FILES["file"]['tmp_name']);
if (!$param_opens) {
print_json(array(call_my_str_code()=> call_my_int_1(), call_my_str_msg()=> "打开文件失败", "url"=> ''));
}
$param_isok=0;
for ($i=0; $i < $param_zip->numFiles; $i++) {
$parram_stat=$param_zip->statIndex($i);
if ($parram_stat['name']=='statics/templates.josn') {
$param_isok=1;
break;
}
}
if ($param_isok==0) {
print_json(array(call_my_str_code()=> call_my_int_aeg1(), call_my_str_msg()=> '上传文件非HDSYSCMS模版'));
}
$param_time=time();
$param_zip_file="cache/zip_hdsyscms_{$param_time}/";
@mkdir(HDSYS_DIR_SYS . $param_zip_file, 0777, true);
$param_zip->extractTo(HDSYS_DIR_SYS . $param_zip_file, array('statics/templates.josn'));
$param_file=HDSYS_DIR_SYS . $param_zip_file . 'statics/templates.josn';
if (!file_exists($param_file)) {
print_json(array(call_my_str_code()=> call_my_int_aeg1(), call_my_str_msg()=> '上传模版无法解析.'));
}
$param_josn=file_get_contents($param_file);
if ($param_josn=='') {
print_json(array(call_my_str_code()=> call_my_int_aeg1(), call_my_str_msg()=> '上传模版无法解析..'));
}
$param_josn=json_decode($param_josn, 1);
if ($param_josn==false) {
print_json(array(call_my_str_code()=> call_my_int_aeg1(), call_my_str_msg()=> '上传模版无法解析...'));
}
$param_id=$param_josn['id'];
if ($param_id=='') {
print_json(array(call_my_str_code()=> call_my_int_aeg1(), call_my_str_msg()=> '上传模版无法解析....'));
}
$param_templates_dir='templates/' . $param_id;
if (file_exists(HDSYS_DIR_SYS . $param_templates_dir)) {
print_json(array(call_my_str_code()=> call_my_int_aeg1(), call_my_str_msg()=> '模版已经存在,无法在安装 模版路径:' . $param_templates_dir));
}
$param_extractTo=$param_zip->extractTo(HDSYS_DIR_SYS . $param_templates_dir . '/');
$param_zip->close();
if ($param_extractTo) {
print_json(array(call_my_str_code()=> 1000, call_my_str_msg()=> '模版安装成功'));
} else {
print_json(array(call_my_str_code()=> call_my_int_aeg1(), call_my_str_msg()=> '模版解压安装失败'));
}
}
}
